﻿//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License
//    Version 2.0.
//
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using Org.BouncyCastle.Math;
using System;
using System.Security.Cryptography;

namespace UProveParams
{

    public class SubgroupRecommendedParameters : RecommendedParameters
    {
        //
        // L = 2048, N = 256
        //
        static class L2048N256
        {

            static public readonly byte[] domainParameterSeed = {
0x22, 0x7c, 0xc8, 0x30, 0x35, 0xac, 0x2c, 0x68,
0xe6, 0xb4, 0xe5, 0xfe, 0x4b, 0x59, 0xc0, 0xa8,
0x4a, 0xe8, 0x03, 0x30, 0xf3, 0x80, 0xde, 0x03,
0x22, 0x3e, 0x37, 0x81, 0x36, 0xd7, 0x6f, 0xc0                                                         
    };

            static public readonly byte[] p = {
0xef, 0x09, 0x90, 0x06, 0x1d, 0xb6, 0x7a, 0x9e,
0xae, 0xba, 0x26, 0x5f, 0x1b, 0x8f, 0xa1, 0x2b,
0x55, 0x33, 0x90, 0xa8, 0x17, 0x5b, 0xcb, 0x3d,
0x0c, 0x2e, 0x5e, 0xe5, 0xdf, 0xb8, 0x26, 0xe2,
0x29, 0xad, 0x37, 0x43, 0x11, 0x48, 0xce, 0x31,
0xf8, 0xb0, 0xe5, 0x31, 0x77, 0x7f, 0x19, 0xc1,
0xe3, 0x81, 0xc6, 0x23, 0xe6, 0x00, 0xbf, 0xf7,
0xc5, 0x5a, 0x23, 0xa8, 0xe6, 0x49, 0xcc, 0xbc,
0xf8, 0x33, 0xf2, 0xdb, 0xa9, 0x9e, 0x6a, 0xd6,
0x6e, 0x52, 0x37, 0x8e, 0x92, 0xf7, 0x49, 0x2b,
0x24, 0xff, 0x8c, 0x1e, 0x6f, 0xb1, 0x89, 0xfa,
0x84, 0x34, 0xf5, 0x40, 0x2f, 0xe4, 0x15, 0x24,
0x9a, 0xe0, 0x2b, 0xf9, 0x2b, 0x3e, 0xd8, 0xea,
0xaa, 0xa2, 0x20, 0x2e, 0xc3, 0x41, 0x7b, 0x20,
0x79, 0xda, 0x4f, 0x35, 0xe9, 0x85, 0xbb, 0x42,
0xa4, 0x21, 0xcf, 0xab, 0xa8, 0x16, 0x0b, 0x66,
0x94, 0x99, 0x83, 0x38, 0x4e, 0x56, 0x36, 0x5a,
0x44, 0x86, 0xc0, 0x46, 0x22, 0x9f, 0xc8, 0xc8,
0x18, 0xf9, 0x30, 0xb8, 0x0a, 0x60, 0xd6, 0xc2,
0xc2, 0xe2, 0x0c, 0x5d, 0xf8, 0x80, 0x53, 0x4d,
0x42, 0x40, 0xd0, 0xd8, 0x1e, 0x9a, 0x37, 0x0e,
0xef, 0x67, 0x6a, 0x1c, 0x3b, 0x0e, 0xd1, 0xd8,
0xff, 0x30, 0x34, 0x0a, 0x96, 0xb2, 0x1b, 0x89,
0xf6, 0x9c, 0x54, 0xce, 0xb8, 0xf3, 0xdf, 0x17,
0xe3, 0x1b, 0xc2, 0x0c, 0x5b, 0x60, 0x1e, 0x99,
0x44, 0x45, 0xa1, 0xd3, 0x47, 0xa4, 0x5d, 0x95,
0xf4, 0x1a, 0xe0, 0x71, 0x76, 0xc7, 0x38, 0x0c,
0x60, 0xdb, 0x2a, 0xce, 0xdd, 0xee, 0xda, 0x5c,
0x59, 0x80, 0x96, 0x43, 0x62, 0xe3, 0xa8, 0xdd,
0x3f, 0x97, 0x3d, 0x6d, 0x4b, 0x24, 0x1b, 0xcf,
0x91, 0x0c, 0x7f, 0x7a, 0x02, 0xed, 0x3b, 0x60,
0x38, 0x3a, 0x01, 0x02, 0xd8, 0x06, 0x0c, 0x27
        };
            static public readonly byte[] q = {
0xc8, 0xf7, 0x50, 0x94, 0x1d, 0x91, 0x79, 0x19,
0x04, 0xc7, 0x18, 0x6d, 0x62, 0x36, 0x8e, 0xc1,
0x9e, 0x56, 0xb3, 0x30, 0xb6, 0x69, 0xd0, 0x87,
0x08, 0xf8, 0x82, 0xe4, 0xed, 0xb8, 0x28, 0x85
        };
        }

        //
        // L = 3072, N = 256
        //
        static class L3072N256
        {

            static public readonly byte[] domainParameterSeed = {
0x31, 0xf2, 0xd6, 0xcf, 0xcd, 0x65, 0x2b, 0x7d,
0xb8, 0x18, 0x6e, 0x84, 0x9d, 0xf1, 0x4b, 0x75,
0x60, 0x40, 0x7b, 0xca, 0x0f, 0x03, 0x04, 0xe0,
0x9e, 0x0d, 0x9d, 0x2c, 0x03, 0xd4, 0xfa, 0x4c
    };


            static public readonly byte[] p = {
0xf3, 0x02, 0x62, 0x0a, 0x44, 0xe7, 0x0f, 0x14,
0xcd, 0x3b, 0x93, 0x6a, 0xd4, 0x02, 0xfa, 0x78,
0x93, 0x2d, 0x1e, 0x84, 0xaa, 0xa0, 0xf4, 0x0e,
0x17, 0x86, 0x62, 0x54, 0xa6, 0x9b, 0x86, 0x7e,
0x8a, 0x6f, 0x27, 0xdc, 0x89, 0x3c, 0x86, 0x4c,
0x9d, 0xa7, 0xd3, 0x58, 0x74, 0xf0, 0xb3, 0x25,
0xca, 0x8c, 0x92, 0x1b, 0xeb, 0xb6, 0x2a, 0x14,
0x53, 0x03, 0x40, 0x52, 0x9f, 0x3e, 0xc3, 0x87,
0x21, 0x11, 0x5a, 0x10, 0x9c, 0xfc, 0x85, 0x4d,
0x2b, 0x38, 0xe5, 0x4b, 0xa6, 0xec, 0xc7, 0xdf,
0x61, 0x7b, 0x01, 0x21, 0xca, 0x30, 0xfb, 0x34,
0x3e, 0x1b, 0xbe, 0xba, 0x1c, 0xd2, 0x5c, 0x68,
0xd4, 0x5b, 0xff, 0x10, 0x25, 0x77, 0xb4, 0x08,
0xe8, 0x59, 0x42, 0xeb, 0x6b, 0xe0, 0x35, 0x23,
0xb3, 0x74, 0x0a, 0x1d, 0x1c, 0x9f, 0xc7, 0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xeb, 0xcd,
0xc8, 0x91, 0x0f, 0x24, 0x17, 0xf8, 0xdb, 0x79,
0x26, 0x6a, 0xda, 0x66, 0x6c, 0xec, 0xc0, 0x45,
0x85, 0xca, 0x01, 0x86, 0xd2, 0xc7, 0xbf, 0xec,
0xa5, 0x17, 0xb1, 0x4c, 0x9d, 0x11, 0xbb, 0x52,
0x7a, 0x61, 0x3a, 0xe1, 0x58, 0x83, 0x12, 0x83,
0xee, 0x1f, 0x49, 0x95, 0x04, 0xbd, 0x5d, 0x8c,
0xb5, 0x93, 0x13, 0x83, 0x53, 0x62, 0x55, 0x9b,
0x1a, 0xf0, 0x22, 0xd9, 0x28, 0x7c, 0x8e, 0x95,
0x36, 0xf7, 0x83, 0x41, 0x06, 0xcb, 0x7c, 0xcf,
0x41, 0x62, 0xd1, 0x4a, 0x89, 0x29, 0x39, 0xad
        };
            static public readonly byte[] q = {
0xf2, 0x05, 0x7b, 0xc9, 0x6a, 0x4f, 0x5b, 0x6f,
0xca, 0x7a, 0xc1, 0xaf, 0x65, 0x3f, 0xe7, 0x6d,
0x07, 0xc1, 0x91, 0x53, 0xf6, 0x25, 0x8c, 0x8a,
0x94, 0x4c, 0x52, 0x7c, 0x51, 0x29, 0xeb, 0xe9
        };
        }

        //
        // L = 1024, N = 160 (for ABC4Trust). P and Q from Credentica spec
        //
        static class L1024N160
        {

            static public readonly byte[] domainParameterSeed = {
 0x42, 0xf3, 0x05, 0xc4, 0x7a, 0xfa, 0xa3, 0x3b,
 0x97, 0xd7, 0x25, 0x77, 0x5c, 0xc2, 0xfe, 0x61,
 0xa8, 0xa1, 0xae, 0xe7
     };

            static public readonly byte[] p = {
0xd2, 0x1a, 0xe8, 0xd6, 0x6e, 0x6c, 0x6b, 0x3c,
0xed, 0x0e, 0xb3, 0xdf, 0x1a, 0x26, 0xc9, 0x1b,
0xde, 0xed, 0x01, 0x3c, 0x17, 0xd8, 0x49, 0xd3,
0x0e, 0xc3, 0x09, 0x81, 0x3e, 0x4d, 0x37, 0x99,
0xf2, 0x6d, 0xb0, 0xd4, 0x94, 0xe8, 0x2e, 0xc6,
0x1e, 0xa9, 0xfd, 0xc7, 0x0b, 0xb5, 0xcb, 0xca,
0xf2, 0xe5, 0xf1, 0x8a, 0x83, 0x64, 0x94, 0xf5,
0x8e, 0x67, 0xc6, 0xd6, 0x16, 0x48, 0x0c, 0x37,
0xa7, 0xf2, 0x30, 0x61, 0x01, 0xfc, 0x9f, 0x0f,
0x47, 0x68, 0xf9, 0xc9, 0x79, 0x3c, 0x2b, 0xe1,
0x76, 0xb0, 0xb7, 0xc9, 0x79, 0xb4, 0x06, 0x5d,
0x3e, 0x83, 0x56, 0x86, 0xa3, 0xf0, 0xb8, 0x42,
0x0c, 0x68, 0x34, 0xcb, 0x17, 0x93, 0x03, 0x86,
0xde, 0xda, 0xb2, 0xb0, 0x7d, 0xd4, 0x73, 0x44,
0x9a, 0x48, 0xba, 0xab, 0x31, 0x62, 0x86, 0xb4,
0x21, 0x05, 0x24, 0x75, 0xd1, 0x34, 0xcd, 0x3b
        };
            static public readonly byte[] q = {
0xff, 0xf8, 0x0a, 0xe1, 0x9d, 0xae, 0xbc, 0x61,
0xf4, 0x63, 0x56, 0xaf, 0x09, 0x35, 0xdc, 0x0e,
0x81, 0x14, 0x8e, 0xb1
        };
        }

        public class SGParams
        {
            public string Oid;
            public BigInteger p;
            public BigInteger q;
            public BigInteger g;
            public byte[] domain_parameter_seed;
            public BigInteger e;
            public BigInteger g_d;
            public BigInteger g_t;
            public BigInteger[] g_i = new BigInteger[NumberOfPregeneratedGenerators];
            internal int[] counter = new int[NumberOfPregeneratedGenerators];
            internal int counter_g;
            internal int counter_d;
            internal int counter_t;
        }


        public enum SGNames { L1024N160 = 0, L2048N256, L3072N256, };
        static public readonly string[] OID = new string[] { "1.3.6.1.4.1.311.75.1.1.0", "1.3.6.1.4.1.311.75.1.1.1", "1.3.6.1.4.1.311.75.1.1.2" };
        static public SGParams[] sgParams = new SGParams[OID.Length];
        static public SGNames GetSGName(string sgName)
        {
            if (sgName == "L1024N160")
            {
                return SGNames.L1024N160;
            }
            else if (sgName == "L2048N256")
            {
                return SGNames.L2048N256;
            }
            else if (sgName == "L3072N256")
            {
                return SGNames.L3072N256;
            }
            else
            {
                throw new ArgumentException("unsupported group: " + sgName);
            }
        }


        static SubgroupRecommendedParameters()
        {
            sgParams[(int)SGNames.L1024N160] = GenerateParameters("L1024N160");
            sgParams[(int)SGNames.L2048N256] = GenerateParameters("L2048N256");
            sgParams[(int)SGNames.L3072N256] = GenerateParameters("L3072N256");
        }

        static public SGParams GenerateParameters(string groupName)
        {
            SGParams sgParams = new SGParams();

            SGNames sgNameEnum = GetSGName(groupName);
            SGParams sgp = SubgroupRecommendedParameters.sgParams[(int)sgNameEnum];
            sgParams.Oid = SubgroupRecommendedParameters.OID[(int)sgNameEnum];

            if (sgNameEnum == SGNames.L1024N160)
            {
                sgParams.p = new BigInteger(1, L1024N160.p);
                sgParams.q = new BigInteger(1, L1024N160.q);
                sgParams.domain_parameter_seed = L1024N160.domainParameterSeed;
            }
            else if (sgNameEnum == SGNames.L2048N256)
            {
                sgParams.p = new BigInteger(1, L2048N256.p);
                sgParams.q = new BigInteger(1, L2048N256.q);
                sgParams.domain_parameter_seed = L2048N256.domainParameterSeed;
            }
            else if (sgNameEnum == SGNames.L3072N256)
            {
                sgParams.p = new BigInteger(1, L3072N256.p);
                sgParams.q = new BigInteger(1, L3072N256.q);
                sgParams.domain_parameter_seed = L3072N256.domainParameterSeed;
            }
            else
            {
                throw new ArgumentException("unsupported group: " + sgNameEnum);
            }

            // g
            sgParams.g = FIPS_186_3_AnnexA_2_3(sgParams.p, sgParams.q, sgParams.domain_parameter_seed, 0, null, out sgParams.counter_g);

            // e = (p-1)/q (for verifiably random group generation)
            sgParams.e = sgParams.p.Subtract(BigInteger.One).Divide(sgParams.q);

            // g_1 ... g_n
            sgParams.g_i = new BigInteger[NumberOfPregeneratedGenerators];
            sgParams.counter = new int[NumberOfPregeneratedGenerators];
            for (int i = 1; i <= NumberOfPregeneratedGenerators; i++)
            {
                sgParams.g_i[i - 1] = FIPS_186_3_AnnexA_2_3(sgParams.p, sgParams.q, sgParams.domain_parameter_seed, (byte)i, null, out sgParams.counter[i - 1]);
            }

            // g_t
            sgParams.g_t = FIPS_186_3_AnnexA_2_3(sgParams.p, sgParams.q, sgParams.domain_parameter_seed, 255, null, out sgParams.counter_t);

            // g_d
            sgParams.g_d = FIPS_186_3_AnnexA_2_3(sgParams.p, sgParams.q, sgParams.domain_parameter_seed, 254, null, out sgParams.counter_d);

            return sgParams;

        }

        private static readonly byte[] ggen = new byte[] { (byte)0x67, (byte)0x67, (byte)0x65, (byte)0x6e };
        public static BigInteger FIPS_186_3_AnnexA_2_3(BigInteger p, BigInteger q, byte[] domain_parameter_seed, byte index, Formatter formater, out int count)
        {

            string hashAlg = null;
            if (q.BitLength >= 512)
            {
                hashAlg = "SHA-512";
            }
            else if (q.BitLength >= 256)
            {
                hashAlg = "SHA-256";
            }
            else if (q.BitLength >= 160)
            {
                hashAlg = "SHA1";
            }
            else
            {
                throw new ArgumentException("q is too small");
            }
            HashAlgorithm hash = HashAlgorithm.Create(hashAlg);

            BigInteger e = p.Subtract(BigInteger.One).Divide(q);
            if (formater != null)
            {
                formater.PrintBigInteger("vr_e", "UCHAR", null, e);
            }
            count = 1;

            while (count != 0)
            {
                int uIndex = 0;
                byte[] U = new byte[domain_parameter_seed.Length + ggen.Length + 2];
                Array.Copy(domain_parameter_seed, 0, U, uIndex, domain_parameter_seed.Length);
                uIndex += domain_parameter_seed.Length;
                Array.Copy(ggen, 0, U, uIndex, ggen.Length);
                uIndex += ggen.Length;
                U[U.Length - 2] = index;
                U[U.Length - 1] = (byte)count;

                BigInteger W = new BigInteger(1, hash.ComputeHash(U));
                BigInteger g = W.ModPow(e, p);
                if (g.CompareTo(BigInteger.Two) >= 0)
                {
                    if (formater != null)
                    {
                        formater.PrintHex("vr_U", "UCHAR", null, U);
                        formater.PrintBigInteger("vr_W", "UCHAR", null, W);
                    }
                    return g;
                }

                count++;
            }

            throw new Exception("Can't generate generator; max count reached");
        }

        internal static void Print(Formatter formatter, string groupName)
        {
            SGNames sgNameEnum = GetSGName(groupName);
            SGParams sgp = SubgroupRecommendedParameters.sgParams[(int)sgNameEnum];
            sgp.Oid = SubgroupRecommendedParameters.OID[(int)sgNameEnum];
            formatter.PrintText("OID", "UCHAR", null, sgp.Oid);
            formatter.PrintBigInteger("p", "UCHAR", null, sgp.p);
            formatter.PrintBigInteger("q", "UCHAR", null, sgp.q);
            formatter.PrintBigInteger("g", "UCHAR", null, sgp.g);
            formatter.PrintHex("domainParamSeed", "UCHAR", null, sgp.domain_parameter_seed);
            formatter.PrintBigInteger("e", "UCHAR", null, sgp.e);

            // g_1 ... g_n
            for (int i = 1; i <= NumberOfPregeneratedGenerators; i++)
            {
                formatter.PrintBigInteger("g" + i, "UCHAR", null, sgp.g_i[i - 1], sgp.counter[i - 1]);
            }

            // g_t
            formatter.PrintBigInteger("gt", "UCHAR", null, sgp.g_t, sgp.counter_t);

            // g_d
            formatter.PrintBigInteger("gd", "UCHAR", null, sgp.g_d, sgp.counter_d);

        }
    }
}
